package 优先算法.递归_搜索与回溯.二叉树深度搜索.验证二叉搜索树;

/**
 *https://leetcode.cn/problems/validate-binary-search-tree/description/
 */
public class Main {
    public static void main(String[] args) {
        Solution so = new Solution();
        Solution.TreeNode root = new Solution.TreeNode(-2147483648);
//        root.left = new Solution.TreeNode(1);
//        root.right = new Solution.TreeNode(3);
//        root.left.left = new Solution.TreeNode(0);
//        root.left.right = new Solution.TreeNode(2);

        boolean rel = so.isValidBST(root);
        System.out.println(rel);
    }
    private static void printTree(Solution.TreeNode node) {
        if (node == null) return;
        System.out.print(node.val + " ");
        printTree(node.left);
        printTree(node.right);
    }
}
class Solution {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    public long prev = Long.MIN_VALUE; ;
    public boolean isValidBST(TreeNode root) {
        if(root == null){
            return true;
        }
        Boolean left = isValidBST(root.left);
        if(left == false)return false;
        boolean cur = false;
        if(prev < root.val){
            cur = true;
        }
        if(cur == false) return false;
        prev = root.val;
        boolean right = isValidBST(root.right);
        if(right == false)return false;
        return cur && left && right;
    }
}
